//
//  IndexMenuBar.swift
//  browser
//
//  Created by Kotori on 2021/6/9.
//

import SwiftUI

struct IndexMenuBar: View {
    @Binding var currURL: String
    @Binding var isLoading: Bool
    @Binding var isBookmarked: Bool
    var tabCount:Int
    var indexCallback: IndexCallback?
    var indexBtnDelegate: IndexBtnDelegate?
    var tabSwitchAction: (()->Void)?
    
    var body: some View {
        
        VStack {
            HStack {
                IndexMenuUser(indexCallback: indexCallback)
                    .frame(width: 30, height: 30)
                    .padding(.leading, 5)
                
               
                TextField("搜索或键入网址", text: $currURL)
                    .frame(height: 30)
                    .multilineTextAlignment(.center)
                    .padding(.horizontal, 10)
                    .background(RoundedRectangle(cornerRadius: 15).foregroundColor(Color(.systemGray5)))
                    .disabled(true)
                    .onTapGesture {
                        indexBtnDelegate?.goToSearchView()
                    }
                
                
                IndexTabButton(tabCount: tabCount, indexCallback: self.indexCallback, tabAction:tabSwitchAction)
                    .padding(.leading, 5)

                IndexMenuList(indexCallback: indexCallback, indexBtnDelegate: indexBtnDelegate, isLoading: $isLoading, isBookmarked: $isBookmarked)
                    .frame(width:25)
                    .padding(.horizontal, 5)
                    
                
            }
            .frame(height: 50)
        }
    }
}

struct IndexTabButton: View {
    var tabCount: Int

    var indexCallback: IndexCallback?
    var tabAction: (()->Void)?
    
    var body: some View {
        ZStack {
            Button(action: {
                //前往标签切换页面
                tabAction?()
                indexCallback?.goToView(to: .TabSwitchView, from: .IndexView, intent: .Default, with: nil)
            }, label: {
                Image(systemName: "app.fill").resizable()
            })
            .frame(width: 24, height: 24)
            .cornerRadius(5)
            Text(String(tabCount)).foregroundColor(.white)
        }
    }
}

struct IndexMenuBar_Previews: PreviewProvider {
    static var previews: some View {
        IndexMenuBar(currURL: Binding.constant(""),isLoading: .constant(false),isBookmarked: .constant(false), tabCount: 1)

    }
}
